﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace FastFood.WindowsService
{
    public enum FlashMode
    {
        FLASHW_CAPTION = 0x1,
        FLASHW_TRAY = 0x2,
        FLASHW_ALL = FLASHW_CAPTION | FLASHW_TRAY
    }

    public struct FlashInfo
    {
        public int cdSize;
        public System.IntPtr hwnd;
        public int dwFlags;
        public int uCount;
        public int dwTimeout;
    }

    public partial class FrmAlerta : Form
    {
        private int X = 0;
        private int Y = 0;

        public FrmAlerta(string mensaje)
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;

            lblMensaje.Text = mensaje;
        }

        private void FrmAlerta_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                X = Screen.GetWorkingArea(this).Width;
                Y = Screen.GetWorkingArea(this).Height;
                this.Location = new Point(X - this.Width, Y + this.Height);

                timer1.Enabled = true;
                timer1.Start();

                AvisoDeMensaje(this.Handle);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int i = this.Location.Y;
            if (i > Y - this.Height)
            {
                this.Location = new Point(X - this.Width, i - 8);
            }
            else
            {
                timer1.Stop();
                timer1.Enabled = false;

                timer3.Start();
                timer3.Enabled = true;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer3.Stop();
            timer3.Enabled = false;

            int i = this.Location.Y;
            if (i < Y)
            {
                this.Location = new Point(X - this.Width, i + 8);
            }
            else
            {
                timer2.Stop();
                timer2.Enabled = false;
                this.Close();
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            timer2.Start();
            timer2.Enabled = true;
        }

        [DllImport("user32.dll")]
        public static extern int FlashWindowEx(ref FlashInfo pfwi);

        public void Flash(IntPtr hwnd, FlashMode flashMode, int times)
        {
            FlashInfo FlashInf = new FlashInfo();
            FlashInf.cdSize = Marshal.SizeOf(FlashInf);
            FlashInf.dwFlags = (int)flashMode;
            FlashInf.dwTimeout = 0;
            FlashInf.hwnd = hwnd;
            FlashInf.uCount = times;
            FlashWindowEx(ref FlashInf);
        }

        public void AvisoDeMensaje(IntPtr intPtr)
        {
            Flash(intPtr, FlashMode.FLASHW_ALL, 3);
        }
    }
}
